/*
 * 
 *
 * Created on March 22, 2007, 12:34 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

//package edu.byu.isys413.pb88.data;
package finalintex2web;
import java.util.*;
import java.io.*;
import java.sql.*;

/**
 *
 * @author Prabol
 */
public class ForRentProductDAO extends PhysicalProductDAO 
{
    
    
    /** Creates a new instance of ForRentProductDAO */
    public ForRentProductDAO()
    {
        super();
    }
    protected static ForRentProductDAO instance = null;
        
    public static synchronized ForRentProductDAO getInstance()
    {
        //System.out.println("For rent DAO get Instance");
        if (instance == null) {
            instance = new ForRentProductDAO();
        }
        return instance;
    }
    
    public ForRentProductBO create()
    {
        String id = GUID.generate();
        // super.create();
        //System.out.println("in create of for rent dao");
        ForRentProductBO rent = new  ForRentProductBO(id);
        Cache c = Cache.getInstance();
        c.put(rent.getId(),rent);
        return rent;
    }
    
    public ForRentProductBO read(String id) throws DataException,ConnectionPoolException
    {
        
        try
        {
            Connection conn = ConnectionPool.getInstance().get();
            ForRentProductBO prod = read(id,conn);
            ConnectionPool.getInstance().release(conn);
            return prod;
        }
        catch (SQLException e)
        {
            // rollback
            throw new DataException("SQL error from read 1", e);
        }
        catch (ConnectionPoolException e)
        {
            throw new ConnectionPoolException("Connection pool error in read 1", e);
        }
    }
    
    
    public ForRentProductBO read(String id, Connection conn) throws SQLException, DataException, ConnectionPoolException{
     if(Cache.getInstance().containsKey(id))
     {
            ForRentProductBO rent = (ForRentProductBO)Cache.getInstance().get(id);
            System.out.println("in for rent read cache");
            return rent;
     } 
                
          try
          { 
             
           PreparedStatement stmt = conn.prepareStatement("select * from product,physical,forrent where productid = physicalid and physicalid = forrentid and forrentid = ?");
           stmt.setString(1,id);
           ResultSet rs =  stmt.executeQuery();
           
           ConnectionPool.getInstance().release(conn);
            System.out.println(rs.toString());
            if (rs.next()) 
            {
                //System.out.println(rs.getString("ForRentProductId"));
                ForRentProductBO forRent = ForRentProductDAO.getInstance().create();
                Cache.getInstance().put(id,forRent);
                super.read(id);
                forRent.setTimesrented(rs.getInt("timesRented")); //name table column
                forRent.setId(rs.getString("ForRentProductId"));
                forRent.setPrice(rs.getDouble("Price"));
                forRent.setDescription(rs.getString("Description"));
                forRent.setCurrentRental(rs.getString("currentRental")); //name table column
                Cache.getInstance().put(rs.getString("ForRentProductId"),forRent);      
                forRent.setIndb(true);
                stmt.close();
                return forRent;

            }
            else
            {
                throw new DataException("ForRentProductBO was not found in the database.");
            }
           
            }
           catch (ConnectionPoolException e)
           {
                throw new ConnectionPoolException("Could not retrieve a connection from the database", e);
            
            }
     catch (SQLException e){
                throw new DataException("SQL error", e);
            }
    }
    
     public synchronized void save(ForRentProductBO forRent) throws DataException,ConnectionPoolException {
            
        try
        {
            Connection conn = ConnectionPool.getInstance().get();
            save(forRent,conn);
            ConnectionPool.getInstance().release(conn);
        }
        catch (SQLException e)
        {
            //rollback
            throw new DataException("Could not retrieve record for id=" + forRent.getId(), e);
            
         }
        catch (ConnectionPoolException e)
        {
            //throw new ConnectionPoolException("Could not retrieve record for id=" + e);
                e.printStackTrace();
        }
        
        // release the connection back to the pool
        return;
    }
     synchronized void save(ForRentProductBO forRent, Connection conn) throws SQLException, DataException, ConnectionPoolException 
     {
        // check the dirty flag in the RevenueSourceDBO.  if it is dirty, 
        // run update or insert
        // System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaam");
        if (forRent.isDirty()) 
        {
            if (forRent.isIndb()) 
            {
               update(forRent, conn);
            }
            else
            {
               insert(forRent, conn);
            }
            forRent.setDirty(false);
        Cache.getInstance().touch(forRent.getId());
     
        }
        
    }

    private synchronized void update(ForRentProductBO forRent, Connection conn) throws SQLException, DataException, ConnectionPoolException
    {
        //System.out.println("in for rent DAO update");
        PreparedStatement stmt = conn.prepareStatement("UPDATE ForRentProduct SET timesrented=? and currentRental=? WHERE forrentid=? ");
        super.save((PhysicalProductBO)forRent,conn);
        ForRentProductBO fr = (ForRentProductBO)forRent;
        //System.out.println("Update for rent");
        stmt.setInt(1,fr.getTimesrented());
        stmt.setString(2,fr.getCurrentRental());
        stmt.setString(3,fr.getId());
        stmt.execute();
        stmt.close();
        ConnectionPool.getInstance().release(conn);
        
    }
    private synchronized void insert(ForRentProductBO forRent, Connection conn) throws SQLException, DataException, ConnectionPoolException
    {
        //System.out.println("in for rent DAO insert");
        PreparedStatement stmt = conn.prepareStatement("INSERT INTO ForRentProduct (forrentid,timesrented,currentrental) VALUES ( ?,?,?)");
        
        super.save((PhysicalProductBO)forRent,conn);
        ForRentProductBO fs = (ForRentProductBO)forRent;
        stmt.setString(1,fs.getId());
        stmt.setInt(2,fs.getTimesrented());
        stmt.setString(3,fs.getCurrentRental());
        stmt.execute();
        stmt.close();
        // tell the RevenueSourceDBO that it's now in the db (so we call update next time not insert)
        forRent.setIndb(true);
        ConnectionPool.getInstance().release(conn);
    }
}
